﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Resources;
using System.Reflection;
using kad2SimSoft.Classes;
using System.Windows.Shapes;
using kad2SimSoft.Properties;
using kad2SimSoft.Resources.Lokalisierung;

namespace kad2SimSoft {
    /// <summary>
    /// Interaktionslogik für PurchasedPartsPage.xaml
    /// </summary>
    public partial class PurchasedPartsPage : Page {
       private List<PurchasedPart> _purchasedParts = new List<PurchasedPart>();
       private ObservableCollection<Point> CollectionPointPurchasedParts = new ObservableCollection<Point>();

        public PurchasedPartsPage (List<PurchasedPart> purchasedParts) {
           InitializeComponent();
           
           if (purchasedParts.Count == 0) {
               throw new NoNullAllowedException();
           }
           else {
               this._purchasedParts = purchasedParts;
           }

           initTextBoxObjects();
       }

       

        void initTextBoxObjects () {
            int topMargin = 85;
            int leftMargin = 30;
            double result;
            foreach (var row in _purchasedParts) {
                #region ID
                CustomTextBox txtBoxId = new CustomTextBox(
                    string.Format("txtBoxId_{0}", row.Id.ToString()),
                    string.Format("Id: {0}", row.Id.ToString()),
                    new Thickness(leftMargin, topMargin, 0, 0),
                    60,
                    string.Format("{0}", enUS.tpIdPurchasedPart.ToString())
                    );
                txtBoxId.Style = txtBoxStyle.Style;
                txtBoxId.TextAlignment = TextAlignment.Left;
                LayoutRoot.RegisterName(txtBoxId.Name, txtBoxId);
                LayoutRoot.Children.Add(txtBoxId);
                #endregion
                #region Price
                CustomTextBox txtBoxPrice = new CustomTextBox(
                    string.Format("txtBoxPrice_{0}", row.Id.ToString()),
                    string.Format("{0:##} €", row.Price.ToString()),
                    new Thickness(leftMargin + 65, topMargin, 0, 0),
                    60,
                    string.Format("{0}", enUS.tpPricePurchasedPart.ToString())
                    );
                txtBoxPrice.filterColours(
                    double.Parse("0,00", System.Globalization.CultureInfo.InvariantCulture),
                    double.Parse("10,00", System.Globalization.CultureInfo.InvariantCulture),
                    double.Parse(row.Price.ToString(), System.Globalization.CultureInfo.InvariantCulture));
                txtBoxPrice.Style = txtBoxStyle.Style;
                LayoutRoot.RegisterName(txtBoxPrice.Name, txtBoxPrice);
                LayoutRoot.Children.Add(txtBoxPrice);
                #endregion
                #region Amount
                CustomTextBox txtBoxAmount = new CustomTextBox(string.Format("txtBoxAmount_{0}", row.Id.ToString()),
                    string.Format("{0}x", row.Amount.ToString()),
                    new Thickness(leftMargin + (2 * 65), topMargin, 0, 0),
                    60,
                    string.Format("{0}", enUS.tpAmountPurchasedPart.ToString())
                    );
                txtBoxAmount.setStyle = txtBoxStyle.Style;
                LayoutRoot.RegisterName(txtBoxAmount.Name, txtBoxAmount);
                LayoutRoot.Children.Add(txtBoxAmount);
                #endregion
                #region AmountPercent
                result = (System.Math.Round(
                    (double)100 /
                    double.Parse(row.StartAmount.ToString(), System.Globalization.CultureInfo.InvariantCulture) *
                    double.Parse(row.Amount.ToString(), System.Globalization.CultureInfo.InvariantCulture)
                    , 1));
                CustomTextBox txtBoxAmountPercent = new CustomTextBox(
                    string.Format("txtBoxAmountinPercent_{0}", row.Id.ToString()),
                    string.Format("{0}%", result.ToString()),
                    new Thickness(leftMargin + (3 * 65), topMargin, 0, 0),
                    60,
                    string.Format("{0}", enUS.tpAmountPercentPurchasedPart.ToString())
                    );
                txtBoxAmountPercent.filterColours((double)25, (double)160, result);
                txtBoxAmountPercent.setStyle = txtBoxStyle.Style;
                LayoutRoot.RegisterName(txtBoxAmountPercent.Name, txtBoxAmountPercent);
                LayoutRoot.Children.Add(txtBoxAmountPercent);
                #endregion
                #region StockCosts
                result = (System.Math.Round(
                    (double.Parse(row.Amount.ToString(), System.Globalization.CultureInfo.InvariantCulture) *
                    double.Parse(row.Price.ToString(), System.Globalization.CultureInfo.InvariantCulture)) / 100
                    , 1));
                CustomTextBox txtBoxStockCost = new CustomTextBox(
                    string.Format("txtBoxStockCost_{0}", row.Id.ToString()),
                    string.Format("{0} €", result.ToString()),
                    new Thickness(leftMargin + (4 * 65), topMargin, 0, 0),
                    60,
                    string.Format("{0}", enUS.tpStockCostPurchasedPart.ToString())
                    );
                txtBoxStockCost.setStyle = txtBoxStyle.Style;
                LayoutRoot.RegisterName(txtBoxStockCost.Name, txtBoxStockCost);
                LayoutRoot.Children.Add(txtBoxStockCost);
                #endregion
                #region Deviation
                CustomTextBox txtBoxProcureLeadTime = new CustomTextBox(
                    string.Format("txtBoxProcureLeadTime_{0}", row.Id.ToString()),
                    row.ProcureLeadTime.ToString(),
                    new Thickness(leftMargin + (5 * 65), topMargin, 0, 0),
                    60,
                    string.Format("{0}", enUS.tpLeadTimePurchasedPart.ToString())
                    );
                txtBoxProcureLeadTime.setStyle = txtBoxStyle.Style;
                LayoutRoot.RegisterName(txtBoxProcureLeadTime.Name, txtBoxProcureLeadTime);
                LayoutRoot.Children.Add(txtBoxProcureLeadTime);
                #endregion
                topMargin += 20;
            }
        }
        void initChartObject () {
            foreach (PurchasedPart purchasedPart in _purchasedParts) {
                CollectionPointPurchasedParts.Add(new Point(purchasedPart.Type + purchasedPart.Id.ToString(), purchasedPart.Price * purchasedPart.Amount));
            }
        }
        private void btOpenPurchasedPartsOverview_Click (object sender, RoutedEventArgs e) {
            PurchasedPartsPopUpWindow wdOverviewPurchasedParts = new PurchasedPartsPopUpWindow(_purchasedParts);
            wdOverviewPurchasedParts.Show();
        }
        
    }
    internal class CustomTextBox : TextBox {
        public CustomTextBox (string customTextBoxName, string text, Thickness margin, int width, string tooltip) {
            TextAlignment = TextAlignment.Right;
            HorizontalAlignment = HorizontalAlignment.Left;
            VerticalAlignment = VerticalAlignment.Top;
            Name = customTextBoxName;
            Text = string.Format("{0}", text);
            Width = width;
            Margin = margin;
            IsReadOnly = true;
            ToolTip _tooltip = new ToolTip { Content = tooltip };
            this.ToolTip = tooltip;
        }

        public void filterColours (double min, double max, double value) {
            if (value < min | value > max) {
                Foreground = Brushes.Red;
            }
            else {
                Foreground = Brushes.Green;
            }
        }

        public Style setStyle {
            get { return this.Style; }
            set { this.Style = value; }
        }

    }
    internal class Point : DependencyObject {
        public static readonly DependencyProperty _date = DependencyProperty.Register("Name", typeof(String), typeof(Point));
        public Point (String date, double value) {
            Date = date;
            Value = value;
        }
        public String Date {
            get { return (String)GetValue(_date); }
            set { SetValue(_date, value); }
        }
        public static readonly DependencyProperty _value = DependencyProperty.Register("Value", typeof(double), typeof(Point));
        public double Value {
            get { return (double)GetValue(_value); }
            set { SetValue(_value, value); }
        }
    }
}
